from typing import List, Optional
from api.base.base_client import BaseClient
from api.models.user import User
from api.models.role import Role


class AclClient(BaseClient):
    """权限管理客户端"""

    def login(self, auth_key: str, password: str) -> str:
        """
        用户登录
        :param auth_key: 认证密钥
        :param password: 密码
        :return: token字符串
        """
        endpoint = "/api/v2/login/enterprise/user_pw"
        data = {
            "authKey": auth_key,
            "passwordx": password
        }

        response = self._post(endpoint, data)
        response.raise_for_status()

        token = response.get_data('_sw_token')
        if not token:
            raise ValueError("登录失败：无法获取token")

        return f"_sw_token={token}"

    def logout(self, token: str) -> dict:
        """
        用户登出
        :param token: 用户token
        :return: 响应数据
        """
        endpoint = f"/api/v2/logout?token={token}"
        response = self._get(endpoint, token)
        response.raise_for_status()
        return response.json()

    def get_user_list(self, token: str) -> List[User]:
        """
        获取用户列表
        :param token: 用户token
        :return: 用户列表
        """
        endpoint = "/api/user/userList"
        response = self._get(endpoint, token)
        response.raise_for_status()

        users_data = response.get_data() or []
        return [User.from_dict(user_data) for user_data in users_data]

    def get_enterprise_info(self, token: str) -> dict:
        """
        获取企业信息
        :param token: 用户token
        :return: 企业信息
        """
        endpoint = "/api/account/customer/enterprise"
        response = self._get(endpoint, token)
        response.raise_for_status()
        return response.get_data() or {}

    def get_current_login_account(self, token: str) -> dict:
        """
        获取当前登录账号信息
        :param token: 用户token
        :return: 账号信息
        """
        endpoint = "/api/uic/get/current/login/account"
        response = self._get(endpoint, token, host=self.account_host)
        response.raise_for_status()
        return response.get_data() or {}

    def create_account(self, token: str, credential: str, customer_type: int,
                       department_ids: List[str], mobile: str, role_ids: List[str],
                       user_name: str) -> dict:
        """
        创建新账号
        :param token: 用户token
        :param credential: 认证信息
        :param customer_type: 客户类型
        :param department_ids: 部门ID列表
        :param mobile: 手机号
        :param role_ids: 角色ID列表
        :param user_name: 用户名
        :return: 创建结果
        """
        endpoint = "/api/account/smart-rongmei/customer/new"
        data = {
            "credential": credential,
            "customerType": customer_type,
            "departmentIds": department_ids,
            "mobile": mobile,
            "roleIds": role_ids,
            "userName": user_name
        }

        response = self._post(endpoint, data, token)
        response.raise_for_status()
        return response.get_data() or {}

    def update_account_status(self, token: str, customer_key: str,
                              blocked: bool) -> dict:
        """
        更新账号状态
        :param token: 用户token
        :param customer_key: 客户密钥
        :param blocked: 是否禁用
        :return: 更新结果
        """
        endpoint = f"/api/account/smart-rongmei/customer/updateStatus/{customer_key}"
        data = {"blocked": blocked}

        response = self._post(endpoint, data, token)
        response.raise_for_status()
        return response.get_data() or {}

    def get_role_list(self, token: str) -> List[Role]:
        """
        获取角色列表
        :param token: 用户token
        :return: 角色列表
        """
        endpoint = "/api/role/smart-rongmei/list"
        response = self._get(endpoint, token)
        response.raise_for_status()

        roles_data = response.get_data() or []
        return [Role.from_dict(role_data) for role_data in roles_data]

    def create_role(self, token: str, name: str, description: str) -> dict:
        """
        创建角色
        :param token: 用户token
        :param name: 角色名称
        :param description: 角色描述
        :return: 创建结果
        """
        endpoint = "/api/role/smart-rongmei/save"
        data = {
            "name": name,
            "desc": description
        }

        response = self._post(endpoint, data, token)
        response.raise_for_status()
        return response.get_data() or {}

    def role_auth(self, token: str, role_id: str, smart_rongmei: List[int],
                  mediax: List[int], magic: List[int]) -> dict:
        """
        角色授权
        :param token: 用户token
        :param role_id: 角色ID
        :param smart_rongmei: 洛书荣美权限
        :param mediax: 媒体权限
        :param magic: 魔方权限
        :return: 授权结果
        """
        endpoint = f"/api/role/smart-rongmei/{role_id}/auth"
        data = {
            "smart-rongmei": smart_rongmei,
            "mediax": mediax,
            "magic": magic
        }

        response = self._post(endpoint, data, token)
        response.raise_for_status()
        return response.get_data() or {}

    def delete_role(self, token: str, role_id: str) -> dict:
        """
        删除角色
        :param token: 用户token
        :param role_id: 角色ID
        :return: 删除结果
        """
        endpoint = f"/api/role/{role_id}"
        response = self._delete(endpoint, token)
        response.raise_for_status()
        return response.get_data() or {}